# Boxplot

theme_set(theme_classic())

g <- ggplot(mpg, aes(class, cty))

# Boxplot
g + geom_boxplot(varwidth = TRUE, fill = "plum") +
    labs(title = "Boxplot",
         subtitle = "City mileage grouped by class of vehicle",
         caption = "mpg",
         x = "Class of Vehicle", y = "City mileage")

# Boxplot with grouped variable
g + geom_boxplot(aes(fill = factor(cyl))) +
    theme(axis.text.x = element_text(angle = 65, vjust = 0.6)) +
    labs(title = "Boxplot",
         subtitle = "City mileage grouped by class of vehicle",
         caption = "mpg",
         x = "Class of Vehicle", y = "City mileage")

# Boxplot and Dotplot
gd <- ggplot(mpg, aes(manufacturer, cty))
gd + geom_boxplot() +
    geom_dotplot(binaxis = "y",
                 stackdir = "center", dotsize = 0.5, fill = "steelblue") +
    theme(axis.text.x = element_text(angle = 65, vjust = 0.6)) +
    labs(title = "Boxplot and dotplot",
         subtitle = "City mileage vs Class: each dot represents 1 row in data",
         caption = "mpg",
         x = "Class of Vehicle", y = "City mileage")

# Tufte boxplot
library(ggthemes)
theme_set(theme_tufte())
gd + geom_tufteboxplot() +
    theme(axis.text.x = element_text(angle = 65, vjust = 0.6)) +
    labs(title = "Tufte Styled Boxplot",
         subtitle = "City mileage grouped by class of vehicle",
         caption = "mpg",
         x = "Class of Vehicle", y = "City mileage")

# Violin plot
theme_set(theme_bw())
g + geom_violin() +
    labs(title = "Violin plot",
         subtitle = "City mileage vs class of vehicle",
         caption = "mpg",
         x = "Class of Vehicle", y = "City mileage")
